<?php

namespace admin;

use \core\Controller;

class Index extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!session('auth') && C('ACTION_NAME') != 'login')
            header('location:'.U('admin/Index/login'));
    }

    public function breadCrumb()
    {
        $html = '<li><a href="'.U('Index/index').'">后台首页</a></li>';
        $html .= '<li>'.html_link(U('Type/datalist', [1, 0]), '#datalist', 'a', '类型管理').'</li>';
        $html .= '<li>';
        $root = sqlAll('select id,name from bg_type where pid=0');
        foreach ($root as $row) {
            $html .= html_link(U('Type/datalist', [1, $row['id'], $row['name']]), '#datalist', 'a', $row['name']).'&emsp;';
        }
        $html .= '</li>';
        return $html;
    }

    public function index()
    {
        $this->assign('title', "管理首页");
        $this->assign('breadcrumb', $this->breadCrumb());
        $this->assign('data', R('Type/datalist', [1, 0]));
        $this->display();
    }

    public function logout()
    {
        session_destroy();
        exit(json_return("退出成功!", U('home/Index/index')));
    }

    public function login()
    {
        if (IS_POST) {
            if ($_POST['username'] == '' || $_POST['password'] == ' ')
                exit(json_return('所有字段不能为空!'));

            $pwd = md5(md5($_POST['password']));

            $sql = sprintf('select id,name,role from bg_user where name="%s" and password="%s"', $_POST['username'], $pwd);
            $usr = sqlRow($sql);
            if (count($usr) == 0)
                exit(json_return('用户名或密码错误!'));

            session('auth', $usr);

            exit(json_return('登录成功!', U('index')));
        }
        $this->assign('close_nav', true);
        $this->assign('close_bread', true);
        $this->assign('title', '登录系统');
        $this->display();
    }

    public function config()
    {
        if (IS_POST) {
            if ($_POST['verify'] !== $_POST['pwd'])
                exit(json_return('两次输入的密码不一致!'));

            $pwd = md5(md5($_POST['pwd']));

            if (isset($_POST['uid']) && $_POST['uid'] != '') {
                $ret = M('bg_user')->update([
                    'name' => $_POST['name'],
                    'password' => $pwd
                ], 'id='.$_POST['uid']);

            } else {
                $ret = M('bg_user')->insert([
                    'name' => $_POST['name'],
                    'phone' => "13500221122",
                    'email' => 'admin@163.com',
                    'password' => $pwd
                ]);
            }

            if (!$ret)
                exit(json_return('操作失败!'));

            exit(json_return([
                'close' => true,
                'msg' => '操作成功!'
            ]));
        }
        $row = sqlRow("select id, name, password from bg_user where role=0");
        if (count($row) == 0)
            $row = ['id' => '', 'name' => '', 'password' => ''];

        $this->assign('row', $row);
        echo $this->fetch();
    }
}

